package cn.yjr.vectori.adapter;

import java.io.IOException;
import java.util.List;

import cn.yjr.vector.ChatActivity;
import cn.yjr.vector.R;
import cn.yjr.vector.tools.BitmapHelper;
import cn.yjr.vector.tools.ChatMessage;
import cn.yjr.vector.tools.FileHelper;
import cn.yjr.vector.tools.ImageChatMessage;
import cn.yjr.vector.tools.TextChatMessage;
import cn.yjr.vector.tools.VoiceChatMessage;
import cn.yjr.vector.tools.YJRApplication;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author vector
 *
 */
public class MessageListAdapter extends BaseAdapter{
	Context context;
	ImageLoader imageLoader;
	DisplayImageOptions options,image_options;
	FileHelper fileHelper;
	boolean playState = false;
	private MediaPlayer mediaPlayer;
	String avatar = ChatActivity.avatar;
	String patient_id;
	public List<ChatMessage> message;
	
	public MessageListAdapter(List<ChatMessage> message,Context context,ImageLoader imageLoader, DisplayImageOptions options,FileHelper fileHelper,String id){
		this.context = context;
		this.imageLoader = imageLoader;
		this.options = options;
		this.fileHelper = fileHelper;
		this.patient_id = id;
		this.message = message;
		image_options = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.drawable.me_jobinfo)
		.showImageForEmptyUri(R.drawable.me_appointment)
		.showImageOnFail(R.drawable.head)
		.cacheInMemory(true)
		.cacheOnDisc(true)
		.considerExifParams(true)
		.bitmapConfig(Bitmap.Config.RGB_565)
		.build();
		
	}
	
	public MessageListAdapter(Context context,ImageLoader imageLoader, DisplayImageOptions options,FileHelper fileHelper,String id){
		this.context = context;
		this.imageLoader = imageLoader;
		this.options = options;
		this.fileHelper = fileHelper;
		this.patient_id = id;
		
	}
	
	public class Holder{
		ImageView head;
		TextView sendMessage;
		TextView date;
		RelativeLayout voice_rl,image_rl;
		ImageView image_imv;
		TextView voice_length;
	}

	@Override
	public int getCount() {
		return message.size();
	}

	@Override
	public Object getItem(int position) {
	
		return message.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		
		Holder holder = null;
		final ChatMessage msg = message.get(position);
		if(!msg.isId(patient_id)){
			//return null;
		}
		String name= msg.getName();
		if(true){
			holder = new Holder();
			if(name.equals("patient")){
				convertView = LayoutInflater.from(context).inflate(R.layout.chatting_item_msg_text_left, null);
			}else if(name.equals("doctor")){
				convertView = LayoutInflater.from(context).inflate(R.layout.chatting_item_msg_text_right, null);
			}
			holder.head = (ImageView) convertView.findViewById(R.id.iv_userhead);
			holder.sendMessage = (TextView) convertView.findViewById(R.id.tv_chatcontent);
			holder.date = (TextView) convertView.findViewById(R.id.tv_sendtime);
			holder.voice_rl = (RelativeLayout) convertView.findViewById(R.id.voice_rl);
			holder.image_rl = (RelativeLayout) convertView.findViewById(R.id.image_rl);
			holder.image_imv = (ImageView) convertView.findViewById(R.id.image_imv);
			holder.voice_length = (TextView) convertView.findViewById(R.id.voice_length_tv);
			convertView.setTag(holder);
		}else{
			//holder = (Holder) convertView.getTag();
		}
		if(name.equals("patient")){
			imageLoader.displayImage(YJRApplication.head_path+avatar, holder.head, options);
		}else{
			imageLoader.displayImage(YJRApplication.head_path+YJRApplication.avatar_path, holder.head, options);
		}
		if(msg.getType()==0){
			holder.sendMessage.setVisibility(View.VISIBLE);
			holder.voice_rl.setVisibility(View.INVISIBLE);
			holder.image_rl.setVisibility(View.INVISIBLE);
			holder.voice_length.setVisibility(View.INVISIBLE);
			//Toast.makeText(context, ((TextChatMessage)msg).getMessage(), Toast.LENGTH_SHORT).show();
			holder.sendMessage.setText(((TextChatMessage)msg).getMessage());
		}else if(msg.getType()==2){
			holder.sendMessage.setVisibility(View.INVISIBLE);
			holder.image_rl.setVisibility(View.INVISIBLE);
			holder.voice_rl.setVisibility(View.VISIBLE);
			holder.voice_length.setVisibility(View.VISIBLE);
			holder.voice_rl.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View arg0) {
					ChatMessage tmp = message.get(position);
					play_voice((VoiceChatMessage)tmp);
					
				}
				
			});
		}else if(msg.getType()==1){
			holder.sendMessage.setVisibility(View.INVISIBLE);
			holder.image_rl.setVisibility(View.VISIBLE);
			holder.voice_rl.setVisibility(View.INVISIBLE);
			ImageChatMessage tmp = (ImageChatMessage) message.get(position);
			String pic_path = tmp.getFileName();
			Bitmap bm = BitmapHelper.getSmallBitmap(pic_path, 100, 100);
			holder.image_imv.setImageBitmap(bm);
			
		}
		holder.date.setText(msg.getTimeStamp());
		return convertView;
	}
	protected void play_voice(VoiceChatMessage tmp) {
		String file_name = tmp.getFilePath();
		if (!playState) {
			mediaPlayer = new MediaPlayer();
			try
			{

				mediaPlayer.setDataSource(fileHelper.appFilePath
		                + "voice/"+file_name);
				mediaPlayer.prepare();
				mediaPlayer.start();
				playState=true;
				//设置播放结束时监听
				mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
					
					@Override
					public void onCompletion(MediaPlayer mp) {
						if (playState) {
							playState=false;
						}
					}
				});
			}
			catch (IllegalArgumentException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			catch (IllegalStateException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			catch (IOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		}else {
			if (mediaPlayer.isPlaying()) {
				mediaPlayer.stop();
				playState=false;
			}else {
				playState=false;
			}
		}
	}
}
